package liumengbaio.lolzhangyoubao.Activity.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

import liumengbaio.lolzhangyoubao.Activity.Activity.AllPlayActivity;
import liumengbaio.lolzhangyoubao.Activity.Adapter.PlayersPublishAdapter;
import liumengbaio.lolzhangyoubao.Activity.InFo.FreeHeroInfo;
import liumengbaio.lolzhangyoubao.Activity.InFo.PlayersPublishInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/5.
 */
public class FragmentHeroPlayerspublish extends Fragment {
    private ListView listView;
    private PlayersPublishInfo playersPublishInfo;
    private PlayersPublishAdapter playersPublishAdapter;
    String id;
    private Button btn_allplay;


    public static FragmentHeroPlayerspublish newInstants(FreeHeroInfo info) {
        FragmentHeroPlayerspublish fragmentheroplay = new FragmentHeroPlayerspublish();
        Bundle bundle = new Bundle();
        bundle.putSerializable("playerspublishinfo",info);
        fragmentheroplay.setArguments(bundle);
        return fragmentheroplay;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_playerspublish, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView(getView());
         initData();
        getDatafromWeb();
    }



    private void initView(View view) {
        listView= (ListView) view.findViewById(R.id.lv_playerspublish);
        btn_allplay= (Button) view.findViewById(R.id.btn_allplay);
        FreeHeroInfo playerspublishinfo = (FreeHeroInfo) getArguments().getSerializable("playerspublishinfo");
        id=playerspublishinfo.getId();

    }

    private void initData() {
        btn_allplay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FreeHeroInfo playerinfo = (FreeHeroInfo) getArguments().getSerializable("playerspublishinfo");
                Intent intent = new Intent(getActivity(), AllPlayActivity.class);
                intent.putExtra("playerinfo",playerinfo);
                startActivity(intent);
            }
        });
    }


    private void getDatafromWeb() {
        new Thread(new Runnable() {
            String url=" http://lol.zhangyoubao.com/apis/rest/PlaysService/userPlayList?order_kind=2&role_id="+id+"&season=8&page=1&i_=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&deviceId=EsgMWh3USt5BgC/cGtS1hKY9wRoLT+mJ6iTQiBqMiVY=&t_=1473498316572&p_=22061&v_=400901&a_=lol&pkg_=com.anzogame.lol&d_=android&osv_=18&cha=ucMartket&u_=&modle_=Coolpad%205311&api=PlaysService.userPlayList&time=1473498316&game=lol&secretId=AKIDz8krbsJ5yKBZQpn74WFkmLPx3gnPhESA&secretVersion=v1.0&nonce=701500&secretSignature=FprgBetKKFJ1oqAKX6b98E/DKTY= ";
            @Override
            public void run() {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(url).build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(getActivity(), "失败了！", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Response response) throws IOException {
                        String result = response.body().string();
                        Gson gson = new Gson();
                        playersPublishInfo = gson.fromJson(result, PlayersPublishInfo.class);
                        if (playersPublishInfo.getData().size() > 0) {
                            handler.sendEmptyMessage(1);
                        } else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                });
            }
        }).start();

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    Toast.makeText(getActivity(), "没有得到数据！", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    playersPublishAdapter = new PlayersPublishAdapter(playersPublishInfo,getActivity());

                    listView.setAdapter(playersPublishAdapter);
                    break;
            }
        }
    };
    }



